
package com.treasure.mall.biz.service.core.sms.impl;

import com.treasure.mall.biz.bo.SmsConfig;
import com.treasure.mall.biz.entity.sms.VerifyCodePO;
import com.treasure.mall.biz.repository.sms.VerifyCodeRepository;
import com.treasure.mall.biz.service.core.sms.SMSService;
import com.treasure.mall.biz.service.core.sms.SmsServiceProvider;
import com.treasure.mall.biz.service.core.sms.SmsVerifyCodeTemplate;
import com.treasure.mall.zbq.common.CompareUtils;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.exception.ApiException;
import com.treasure.mall.zbq.common.ienum.sms.SmsTemplateSceneType;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 短信接口
 *
 * @author guomw
 * @date 11/11/2018
 */
@Service
public class SMSServiceImpl implements SMSService {

    @Autowired
    private VerifyCodeRepository verifyCodeRepository;

    @Autowired
    private SmsConfig smsConfig;

    @Override
    public boolean sendSmsVerifyCode(String mobile, SmsTemplateSceneType sceneType) {
        VerifyCodePO verifyCode = verifyCodeRepository.findByMobile(mobile);
        if (verifyCode != null) {
            //判断每个手机的发送间隔时间不能低于1分钟
            if (CompareUtils.compareToDate(verifyCode.getCreateTime(), 60)) {
                throw new ApiException(ApiResult.errorWith(ResultCodeEnum.SMS_TOO_OFTEN_FAILED));
            }
        }
        String code = RandomStringUtils.randomNumeric(Constant.VERIFY_CODE_LENGTH);
        SmsVerifyCodeTemplate smsVerifyCodeTemplate = new SmsVerifyCodeTemplate(sceneType);
        smsVerifyCodeTemplate.setCode(code);
        if (verifyCode == null) {
            verifyCode = new VerifyCodePO();
        }
        verifyCode.setVerifyCode(code);
        verifyCode.setMobile(mobile);
        verifyCode.setCreateTime(LocalDateTime.now());
        verifyCode.setUseStatus(false);
        verifyCodeRepository.save(verifyCode);

        if (SmsServiceProvider.send(mobile, smsVerifyCodeTemplate, smsConfig)) {
            return true;
        } else {
            throw new ApiException(ApiResult.errorWith(ResultCodeEnum.SMS_FAILED));
        }
    }

    @Override
    public boolean checkVerifyCode(String mobile, String verifyCode) {
        VerifyCodePO code = verifyCodeRepository.findByMobile(mobile);
        return code != null && !code.isUseStatus() && CompareUtils.compareToDate(code.getCreateTime(), Constant.EXPIRE_IN) && code.getVerifyCode().equals(verifyCode);
    }

    /**
     * 修改使用状态
     *
     * @param mobile
     */
    @Override
    public void modifyVerifyUseStatus(String mobile) {
        VerifyCodePO code = verifyCodeRepository.findByMobile(mobile);
        if (code != null) {
            code.setUseStatus(true);
            verifyCodeRepository.save(code);
        }
    }
}
